class LinksController < ApplicationController
  # GET /links
  # GET /links.xml
  before_filter :admin?,:except=>[:new,:show]
  before_filter :login_required,:only=>[:new,:show]
  layout "admin"# lambda{|controller| ["new","show"].include?(controller.action_name) ? "front_page" : "admin"}
  cache_sweeper :link_sweeper,:only=>[:update,:destroy]
  def index
    @links = Link.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @links }
    end
  end

  # GET /links/1
  # GET /links/1.xml
  def show
    @link = Link.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @link }
    end
  end

  # GET /links/new
  # GET /links/new.xml
  def new
    @link = Link.new
    @page_title="添加友情链接"

    @page_nav=">&nbsp;"+@page_title
    respond_to do |format|
      format.html {render :layout=>"front_page"}
      format.xml  { render :xml => @link }
    end
  end

  # GET /links/1/edit
  def edit
    @link = Link.find(params[:id])
  end

  # POST /links
  # POST /links.xml
  def create
    @link = Link.new(params[:link])
    @link.user_id=session[:user_id]
    @page_title="添加友情链接"

    @page_nav=">&nbsp;"+@page_title
    respond_to do |format|
      if @link.save
        flash[:notice] = '链接添加成功，请等待验证.'
        format.html { redirect_to("/") }
        format.xml  { render :xml => @link, :status => :created, :location => @link }
      else
        format.html { render :action => "new",:layout=>"front_page" }
        format.xml  { render :xml => @link.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /links/1
  # PUT /links/1.xml
  def update
    @link = Link.find(params[:id])

    respond_to do |format|
      if @link.update_attributes(params[:link])
        flash[:notice] = 'Link was successfully updated.'
        format.html { redirect_to(links_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @link.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /links/1
  # DELETE /links/1.xml
  def destroy
    @link = Link.find(params[:id])
    @link.destroy

    respond_to do |format|
      format.html { redirect_to(links_url) }
      format.xml  { head :ok }
    end
  end
end
